import { useEffect, useState } from "react"
import {DetailDataType,fetchGetDetailAPI} from '@/apis/detail_api'
import {useSearchParams} from 'react-router-dom'



export const useDetail =()=>{
    //文章详情状态数据
    const [detail,setDetail] = useState<DetailDataType|null>(null)
    //获取请求参数
    const [params] = useSearchParams()
    const art_id = params.get('art_id')

    //初始化数据
    useEffect( ()=>{
        const getDetail = async ()=>{
          try {
              //为空值的处理方式 art_id! 断言
           const res = await fetchGetDetailAPI(art_id!)
           //console.log(res.data.data)
           setDetail(res.data.data)
          } catch (error) {
            throw new Error("fetch detail error " + error)
          }
        }
        getDetail()
    },[art_id])

   

    return {
        detail
    }
}